package com.ruoyi.procurement.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.BaseUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.procurement.domain.PsGoods;
import com.ruoyi.procurement.domain.PsGoodsType;
import com.ruoyi.procurement.domain.vo.PsOutboundVo;
import com.ruoyi.procurement.service.IPsGoodsService;
import com.ruoyi.procurement.service.IPsGoodsTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.procurement.mapper.PsOutboundMapper;
import com.ruoyi.procurement.domain.PsOutbound;
import com.ruoyi.procurement.service.IPsOutboundService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 物品出库Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-10-10
 */
@Service
public class PsOutboundServiceImpl implements IPsOutboundService 
{
    private static Map<String,Integer> map = new LinkedHashMap<>();
    @Autowired
    private PsOutboundMapper psOutboundMapper;
    @Autowired
    private IPsGoodsService iPsGoodsService;
    @Autowired
    private IPsGoodsTypeService iPsGoodsTypeService;

    static {
        map.put("meat",0);
        map.put("vegetable",1);
        map.put("non_staple_food",2);
        map.put("fruit",3);
    }
    /**
     * 查询物品出库
     * 
     * @param id 物品出库主键
     * @return 物品出库
     */
    @Override
    public PsOutbound selectPsOutboundById(String id)
    {

        return psOutboundMapper.selectPsOutboundById(id);
    }

    /**
     * 查询物品出库列表
     * 
     * @param psOutbound 物品出库
     * @return 物品出库
     */
    @Override
    public List<PsOutboundVo> selectPsOutboundList(PsOutbound psOutbound,String order)
    {
        Map<String, Object> params = psOutbound.getParams();
        Object psUnitId1 = params.get("psUnitId");
        if (psUnitId1!=null){

            String psUnitId = params.get("psUnitId").toString();
            if (StringUtils.isNotEmpty(psUnitId)){
                String[] split = psUnitId.split(",");
                List<String> strings = new ArrayList<String>(Arrays.asList(split));
                params.put("psUnitId",strings);
            }
        }


        Object goodsTypeId1 = params.get("goodsTypeId");
        if (goodsTypeId1!=null){

            String goodsTypeId = params.get("goodsTypeId").toString();
            if (StringUtils.isNotEmpty(goodsTypeId)){
                String[] split = goodsTypeId.split(",");
                List<String> strings = new ArrayList<String>(Arrays.asList(split));
                params.put("goodsTypeId",strings);
            }
        }
        Object ids1 = params.get("ids");
        if (ids1!=null){
            String ids = params.get("ids").toString();
            if (StringUtils.isNotEmpty(ids)){
                String[] split = ids.split(",");
                List<String> strings = new ArrayList<String>(Arrays.asList(split));
                params.put("ids",strings);
            }
        }
        List<PsOutbound> psOutbounds = psOutboundMapper.selectPsOutboundList(psOutbound, order);
        ArrayList<PsOutboundVo> psOutboundVos = new ArrayList<>();



        for (int i = 0; i < psOutbounds.size(); i++) {
            PsOutbound psOutbound1 = psOutbounds.get(i);
            PsOutboundVo v = new PsOutboundVo();
            BeanUtil.copyProperties(psOutbound1,v, CopyOptions.create().ignoreError());
            if (StringUtils.isNotEmpty(v.getGoodsType())) {
                String goodsType = v.getGoodsType();
                Integer integer = map.get(goodsType);
                if (integer!=null) {
                    v.setSort(integer);
                }else {
                    v.setSort(5);
                }
            }else {
                v.setSort(5);
            }
            psOutboundVos.add(v);
        }

        List<PsOutboundVo> collect = psOutboundVos.stream().sorted((o1, o2) -> o1.getSort() - o2.getSort()).collect(Collectors.toList());

//        for (PsOutbound outbound : psOutbounds) {
//            String psGoodsId = outbound.getPsGoodsId();
//            PsGoods psGoods = iPsGoodsService.selectPsGoodsByGoodsId(psGoodsId);
//
//            PsGoodsType psGoodsType = iPsGoodsTypeService.selectPsGoodsTypeById(psGoods.getGoodsTypeId());
//            outbound.setGoodsTypeId(psGoods.getGoodsTypeId());
//            outbound.setGoodsType(psGoods.getGoodsType());
//            outbound.setPstSpecification(psGoodsType.getPstSpecification());
//            psOutboundMapper.updateById(outbound);
//        }
        return collect;
    }

    /**
     * 新增物品出库
     * 
     * @param psOutbound 物品出库
     * @return 结果
     */
    @Override
    @Transactional
    public int insertPsOutbound(PsOutbound psOutbound)
    {
        psOutbound.setCreateByName(SecurityUtils.getLoginUser().getUser().getNickName());
        psOutbound.setCreateTime(DateUtils.getNowDate());
        if (psOutbound.getPoNum()==null || psOutbound.getPoNum().doubleValue()<=0) {
            throw new ServiceException("请设置正确的出库数量!");
        }
        if (psOutbound.getPoPrice()==null || psOutbound.getPoPrice().doubleValue()<=0) {
            psOutbound.setPoAmount(new BigDecimal("0.0"));
        }else {
            //计算金额
            psOutbound.setPoAmount(psOutbound.getPoNum().multiply(psOutbound.getPoPrice()).setScale(2, RoundingMode.HALF_UP));
        }
        iPsGoodsService.deduction(null,"出库",null,null,psOutbound);



        return psOutboundMapper.insert(psOutbound);
    }

    @Override
    public List<PsOutbound> selectTodayPsOut(Date date, String psGoodsId, String psUnitId) {
        return psOutboundMapper.selectTodayPsOut(date, psGoodsId, psUnitId);
    }

    /**
     * 修改物品出库
     * 
     * @param psOutbound 物品出库
     * @return 结果
     */
    @Override
    public int updatePsOutbound(PsOutbound psOutbound)
    {
        psOutbound.setUpdateTime(DateUtils.getNowDate());
        return psOutboundMapper.updateById(psOutbound);
    }

    /**
     * 批量删除物品出库
     * 
     * @param ids 需要删除的物品出库主键
     * @return 结果
     */
    @Override
    public int deletePsOutboundByIds(String[] ids)
    {
        for (String id : ids) {
            //删除加库存
            PsOutbound psOutbound = psOutboundMapper.selectById(id);
            //加库存
            iPsGoodsService.deduction(null,"出库回退",null,null,psOutbound);
        }
        return psOutboundMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除物品出库信息
     * 
     * @param id 物品出库主键
     * @return 结果
     */
    @Override
    public int deletePsOutboundById(String id)
    {
        //删除加库存
        PsOutbound psOutbound = psOutboundMapper.selectById(id);
        //加库存
        iPsGoodsService.deduction(null,"出库回退",null,null,psOutbound);
        return psOutboundMapper.deleteById(id);
    }

    @Override
    public void deleteByPsGoodsId(String goodsId) {
        psOutboundMapper.deleteByPsGoodsId(goodsId);
    }
}
